import 'dart:async';

import 'package:mybase/base/tool/pprint/FFLog.dart';

/*
  用法举例
  
  List<IProductDetails> result = await FFTimeout(
    onTask: Future(() {
      return BasePayPlatform.instance.queryProductDetails(
        identifiers,
        priorityTrial: priorityTrial,
        getAll: getAll,
      );
    }),
    timeOutValue: list,
    mill: mill,
  ).start();
  return result;
 */


/// 如果任务超时了，就返回指定的默认值
class FFTimeout<T> {
  // 任务
  Future<T> onTask;

  // 延时,默认2秒,单毫秒
  int mill;

  // 超时返回值
  T timeOutValue;

  FFTimeout({
    required this.onTask,
    required this.timeOutValue,
    this.mill = 20000,
  });

  Future<T> start() async {
    Future<T> onTask2 = Future.delayed(Duration(milliseconds: mill), () {
      return timeOutValue;
    });
    return await Future.any<T>([onTask, onTask2]);
  }
}
